<template>
  <div>
    <div class="header">
      <div class="title"><text>课程管理</text></div>
      <el-button type="primary" class="el-icon-circle-plus-outline" size="small" style="margin-left: 40px;"
        @click="dialogFormVisible = true">添加课程
      </el-button>
        <el-form-item label="" size="mini">
          <el-input v-model="queryinfo.name" style="margin-top: 9%; margin-left: 20px;" placeholder="输入名称查询"></el-input>
        </el-form-item>
        <el-button type="primary" size="mini" style="margin-left: 20px;" @click="coureslist">查询</el-button>
    </div>
    <!-- 列表 -->
    <el-card class="box-card">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="课程名称" width="180">
        </el-table-column>
        <el-table-column prop="pricetype" label="收费模式" width="180">
        </el-table-column>
        <el-table-column prop="price" label="单价">
        </el-table-column>
        <el-table-column prop="companyid" label="上课模式">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" @click="getupdate(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="coursedelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryinfo.page"
      :page-sizes="[6, 1, 5, 3]"
      :page-size="queryinfo.psize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <!-- 添加 -->
    <el-dialog title="添加" v-model="dialogFormVisible">
        <el-form ref="form" :model="addform" :label-position="labelPosition">
            <el-form-item label="课程名称">
                <el-input v-model="addform.name" style="margin-right: 177px; width: 300px;" size="small"></el-input>
              </el-form-item>
              <el-form-item label="收费模式">
                <el-radio-group  v-model="addform.pricetype" style="margin-right: 270px; width: 300px;">
                  <el-radio label="按课时收费"></el-radio>
                  <el-radio label="按期收费"></el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="单价">
                <el-input v-model="addform.price" style="margin-right: 155px; width: 300px;" size="small"></el-input>
              </el-form-item>
              <el-form-item label="上课模式">
                <el-radio-group v-model="addform.mode" style="margin-right: 270px; width: 300px;">
                  <el-radio label="一对一"></el-radio>
                  <el-radio label="集体班"></el-radio>
                </el-radio-group>
              </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="couresinsert('form')">确 定</el-button>
          </span>
        </template>
      </el-dialog>
      <!-- 修改 -->
      <el-dialog title="更新" v-model="dialogFormVisibleupdate">
        <el-form ref="formupdate" :model="addform" :label-position="labelPosition">
            <el-form-item label="课程名称">
                <el-input v-model="update.name" style="margin-right: 177px; width: 300px;" size="small"></el-input>
              </el-form-item>
              <el-form-item label="收费模式">
                <el-radio-group  v-model="update.pricetype" style="margin-right: 270px; width: 300px;">
                  <el-radio label="按课时收费"></el-radio>
                  <el-radio label="按期收费"></el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="单价">
                <el-input v-model="update.price" style="margin-right: 155px; width: 300px;" size="small"></el-input>
              </el-form-item>
              <el-form-item label="上课模式">
                <el-radio-group v-model="update.mode" style="margin-right: 270px; width: 300px;">
                  <el-radio label="一对一"></el-radio>
                  <el-radio label="集体班"></el-radio>
                </el-radio-group>
              </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogFormVisibleupdate = false">取 消</el-button>
            <el-button type="primary" @click="updatex('formupdate')">确 定</el-button>
          </span>
        </template>
      </el-dialog>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        labelPosition:'left',
        dialogFormVisible:false,
        dialogFormVisibleupdate:false,
        tableData: [],
        queryinfo:{
          page:1,
          psize:5,
          name:''
        },
        total:0,
        addform:{
        },
        update:{
            id:'',
            name:'',
            pricetype:'',
            price:'',
            mode:''
        },
      }
    },
    created() {
      this.coureslist()
    },
    methods: {
        // 修改
        getupdate(val){
          console.log(val)
          this.dialogFormVisibleupdate=true
          this.update.id=val.id
        // console.log(this.update.id)
        this.tableData.forEach(item=>{
            if(val.id==item.id){
            this.update.name=item.name
            // console.log(item)
            this.update.pricetype=item.pricetype
            this.update.price=item.price
            this.update.mode=item.mode
            }
        })
          
        },
       async updatex(){
            const {data:res} = await this.$http.post('http://www.eshareedu.cn/edusystems/api/courses/add',this.update)
            console.log(res)
            let self=this
            if(res.errCode==10000){
                this.$message.success('更新成功')
                this.dialogFormVisibleupdate=false
                this.coureslist()
                // this.getupdate()
                
            }
        },
        // 添加
         async couresinsert(){
             let self=this
             const {data:res} = await this.$http.post('http://www.eshareedu.cn/edusystems/api/courses/add',this.addform)
            //  console.log(res)
             if(res.errCode==10000){
                 self.$message.success('添加成功')
                 self.dialogFormVisible=false
                 self.coureslist()
             }else{
                 self.$message.error('添加失败')
             }
         },
      //   删除
      async coursedelete(row) {
        // console.log(row)
        const cath = await this.$confirm('确定要删除这条数据吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(error => {
          return error
        })
        // console.log(cath)
        if(cath=='confirm'){
            let self=this
            const {data:res} = await this.$http.get('http://www.eshareedu.cn/edusystems/api/courses/delete?id='+row.id)
            // console.log(res)
            if(res.errCode==10000){
                self.$message.success('删除成功')
                self.coureslist()
            }
        }
        if(cath=='cancel'){
            this.$message.info('已取消删除')
        }

      },
      handleSizeChange(val) {
        this.queryinfo.psize=val
        this.coureslist()
        // console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.queryinfo.page=val
        this.coureslist()
        // console.log(`当前页: ${val}`);
      },
      // 列表
      async coureslist() {
        const {
          data: res
        } = await this.$http.get('http://www.eshareedu.cn/edusystems/api/courses/list',{params:this.queryinfo})
        // console.log(res);
        if (res.errCode == 10000) {
          this.tableData = res.data.list
          this.total=res.data.counts
        } else {
          this.$message.error('请求列课程表失败')
        }

      }
    }
  }

</script>
<style scoped>
  .header {
    width: 100%;
    display: flex;
    height: 50px;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    box-shadow: 1px 1px 3px 1px rgb(200, 226, 241);
    background-color: rgb(233, 237, 240);
    align-items: center;
  }

  .title {
    width: 100px;
    height: 100%;
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
    background-color: rgb(160, 197, 240);
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 17px;
    font-weight: bold;
  }

  .box-card {
    margin-top: 10px;
  }

</style>
